﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace ERPTemplate.Template.MultiTable
{
    public partial class MultiTableMainEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (ValidateParams() == false)
            {
                return;
            }

            if (!IsPostBack)
            {
                BindMainData();
            }
        }
        /// <summary>
        /// 验证参数
        /// </summary>
        /// <returns></returns>
        protected bool ValidateParams()
        {
            bool result = true;
            result &= ((Request["type"] == "add") || (Request["type"] == "edit" && Request["recid"] != null));
            return result;
        }
        /// <summary>
        /// 绑定数据
        /// </summary>
        protected void BindMainData()
        {
            RuleCode.Table.RecordSetPermissions t = new RuleCode.Table.RecordSetPermissions();
            RuleQueryTable1.SetQueryTable(t, this.Page.Title);
            RuleQueryTable1.PageSize = 20;
            WebPager1.PageSize = RuleQueryTable1.PageSize;

            string js = "";
            js += "<script type=\"text/javascript\">";
            js += "var tableName = \""+t.Property.Name .ToString()+"\";";

            if (Request["type"] == "add")
            {
                RuleButton_Modify.Text = "增加";
                RuleCode.Table.User u = new RuleCode.Table.User();
                RuleGroup1.IsInsert = true;
                RuleGroup1.SetTableFieldGroup(u, "");
                RuleQueryTable1.FixedRange = "1=2";
                addData.Visible = false;
                js += "var defValue = \"\";";
            }
            else if (Request["type"] == "edit")
            {
                RuleButton_Modify.Text = "修改";
                RuleCode.Table.User u = RuleCode.Table.User.Find("RECID=" + Request["recid"]);
                RuleGroup1.SetTableFieldGroup(u, "");
                RuleQueryTable1.FixedRange = " userid = '"+u.Userid.ToString()+"'";
                js += "var defValue = \"&def_Userid=" + u.Userid.ToString() + "\";";
            }
            js += "</script>";
            Literal_js.Text = js;
            BindDetailData(0);
        }








        private void BindDetailData(Int32 _pageindex)
        {
            RuleQueryTable1.run(_pageindex);
            WebPager1.DT = RuleQueryTable1.DT;
            WebPager1.DataBind();

        }
        protected void Button_modify_Click(object sender, EventArgs e)
        {
            if (Request["type"] == "add")
            {
                RuleCode.Table.User u = new RuleCode.Table.User();
                u.New();
                RuleGroup1.GetTableFieldGroupValue(u);
                u.Modify();
                string url = this.Page.Request.Url.LocalPath + "?type=edit&recid=" + u.RECID.ToString();
                Page.Response.Redirect(url);
            }
            else if (Request["type"] == "edit")
            {
                RuleCode.Table.User u = RuleCode.Table.User.Find("RECID=" + Request["recid"]);
                RuleGroup1.GetTableFieldGroupValue(u);
                u.Modify();

            }


        }
        protected void RuleQueryTable1_ButtonClick(object sender, EventArgs e)
        {
            BindDetailData(0);
        }

        protected void WebPager1_PageIndexChanged(object sender, RuleCode.Controls.PageChangedEventArgs e)
        {
            WebPager1.CurrentPageIndex = e.NewPageIndex;
            BindDetailData(e.NewPageIndex);
        }
       




        
    }
}
